<?php

namespace app\common\model;

use think\Model;
use think\facade\Db;

/**
 * 基础模型
 */
class Base extends Model {

    /**
     * @var string 数据表名
     */
    protected $table = null;

    /**
     * @var string 主表主键
     */
    protected $table_pk = 'id';

    /**
     * @var string 主表别名
     */
    protected $table_alias = 'a';

    /**
     * @var string 主表字段
     */
    protected $table_field = '';

    /**
     * @var Query 查询构造器
     */
    protected $query = null;

    /**
     * Model constructor.
     * @param string $table
     */
    public function __construct($table = null) {
        !is_null($table) && $this->table = $table;
        if (!is_null($this->table)) {
            is_null($this->query) && $this->query = $this->_query($this->table);
        }
    }

    /**
     * Query call.
     * @param string $name 调用方法名称
     * @param array $args 调用参数内容
     * @return Query
     */
    public function __call($name, $args) {
        if (is_callable($callable = [$this->query, $name])) {
            call_user_func_array($callable, $args);
        }
        return $this;
    }

    /**
     * 数据表名
     * @param string $table 
     * @return string
     */
    private function _table($table = null) {
        return !is_null($table) ? $table : $this->table;
    }

    /**
     * 模型对象
     * @param Query|string $table
     * @return object
     */
    public function _model($table = null) {
        return is_string($table) ? Db::name($this->_table($table)) : $table;
    }

    /**
     * 查询构造器
     * @param string $table
     * @return Query
     */
    private function _query($table = null) {
        return $this->_model($table)->alias($this->table_alias);
    }

    /**
     * 默认主表字段
     * @param string|array|Expression $field
     * @return mixed
     */
    public function _defaultField($field = null) {
//        $this->table_field = !empty($field) ? $field : "{$this->table_alias}.{$this->table_pk}";
        if (!empty($field)) {
            $this->table_field = $field;
        } else {
            if (!in_array($this->table_pk, $this->query->getTableFields())) {
                $this->table_field = "{$this->table_alias}.{$this->query->getPk()}";
            } else {
                $this->table_field = "{$this->table_alias}.{$this->table_pk}";
            }
        }
    }

    /**
     * 获取最近一次查询的sql语句
     * @return string
     */
    public function _getLastSql() {
        return $this->query->getLastSql();
    }

    /**
     * 插入数据
     * @param Query|string $table 数据查询对象
     * @param array $data 数据
     * @param bolean $strict 是否严格检查
     * @return mixed
     */
    public function _insert($table, $data = [], $strict = true) {
        $model = $this->_model($table);
        if (!isset($data['create_time']) && in_array('create_time', $model->getTableFields())) {
            $data['create_time'] = datetime();
        }
        if (!isset($data['create_uid']) && in_array('create_uid', $model->getTableFields()) && self::getModule() === '/admin') {
            $data['create_uid'] = user_id();
        }
        return $model->strict($strict)->insertGetId($data);
    }

    /**
     * 批量插入数据
     * @param Query|string $table 数据查询对象
     * @param array $data 数据
     * @return mixed
     */
    public function _insertAll($table, $data = []) {
        return $this->_model($table)->insertAll($data);
    }

    /**
     * 更新数据
     * @param Query|string $table 数据查询对象
     * @param array $data 数据
     * @param array $where where条件
     * @param bolean $strict 是否严格检查
     * @return mixed
     */
    public function _update($table, $data = [], $where = [], $strict = true) {
        if (!is_array($where)) {
            $table_pk = !stripos($where, ',') ? $where : explode(',', $where);
            $where = [$this->table_pk => $table_pk];
        }
        $model = $this->_model($table);
        if (!isset($data['update_time']) && in_array('update_time', $model->getTableFields())) {
            $data['update_time'] = datetime();
        }
        if (!isset($data['update_uid']) && in_array('update_uid', $model->getTableFields()) && self::getModule() === '/admin') {
            $data['update_uid'] = user_id();
        }
        return $model->strict($strict)->where($where)->update($data) !== false;
    }

    /**
     * 批量更新数据
     * @param Query|string $table 数据查询对象
     * @param array $data 数据(二维数组)
     * @param string $pk 条件主键限制
     * @param array $where where条件
     * @return boolean
     */
    public function _updateAll($table, $data, $pk = 'id', $where = []) {
        list($case, $input) = [[], []];
        foreach ($data as $row)
            foreach ($row as $key => $value) {
                if (stripos($key, '@') !== false) {
                    $key = substr($key, 1);
                } else {
                    $value = "'{$value}'";
                }
                $case[$key][] = "WHEN '{$row[$pk]}' THEN {$value}";
            }
        if (isset($case[$pk]))
            unset($case[$pk]);
        $model = $this->_model($table);
        foreach ($case as $key => $value)
            $input[$key] = $model->raw("CASE `{$pk}` " . join(' ', $value) . ' END');
        return $model->whereIn($pk, array_unique(array_column($data, $pk)))->where($where)->update($input) !== false;
    }

    /**
     * 删除数据
     * @param Query|string $table 数据查询对象
     * @param array|int|string $where where条件
     * @return mixed
     */
    public function _delete($table, $where = []) {
        if (!is_array($where)) {
            $table_pk = !stripos($where, ',') ? $where : explode(',', $where);
            $where = [$this->table_pk => $table_pk];
        }
        $model = $this->_model($table)->where($where);
        if (in_array('is_delete', $model->getTableFields())) {
            $data['is_delete'] = 1;
            in_array('delete_time', $model->getTableFields()) && $data['delete_time'] = datetime();
            (in_array('delete_uid', $model->getTableFields()) && self::getModule() === '/admin') && $data['delete_uid'] = user_id();
            $result = $model->update($data) !== false;
        } else {
            $result = $model->delete();
        }
        return $result;
    }

    /**
     * 数据自增或自减
     * @param Query|string $table 数据查询对象
     * @param int|array $where where条件
     * @param string $fields 表字段
     * @param string $method 操作方法
     * @param string $step 增长值
     * @param string $alias 别名分割符
     * @return mixed
     */
    public function _incOrDec($table, $where = [], $fields = '', $method = 'inc', $step = 1, $alias = '#') {
        !is_array($where) && $where = [$this->table_pk => $where];
        $model = $this->_model($table)->where($where);
        if (stripos($fields, ',') === false) {
            if (stripos($fields, $alias) !== false) {
                list($fields, $method) = explode($alias, $fields);
            }
            $model->$method($fields, $step);
        } else {
            foreach (explode(',', $fields) as $field) {
                if (stripos($field, $alias) !== false) {
                    list($field, $method) = explode($alias, $field);
                    if (stripos($method, '|') !== false) {
                        list($method, $step) = explode('|', $method);
                    }
                }
                $model->$method($field, $step);
            }
        }
        return $model->update() !== false;
    }

    /**
     * 查询单个记录
     * @param string|array|Expression $field 查询字段
     * @return array
     */
    public function _find($field = '') {
        !empty($field) && $this->query->field($field);
        return $this->query->find();
    }

    /**
     * 查询多个记录
     * @param string|array|Expression $field 查询字段
     * @return array
     */
    public function _select($field = '') {
        !empty($field) && $this->query->field($field);
        return $this->query->select()->toArray();
    }

    /**
     * 获取某个列的值
     * @param string|array|Expression $field 查询字段
     * @return array
     */
    public function _column($field = '') {
        $this->_defaultField($field);
        return $this->query->column($this->table_field);
    }

    /**
     * 获取某个字段的值
     * @param string|array|Expression $field 查询字段
     * @return mixed
     */
    public function _value($field = '') {
        $this->_defaultField($field);
        return $this->query->value($this->table_field);
    }

    /**
     * Count统计查询
     * @param string|array|Expression $field 查询字段
     * @return float|string
     */
    public function _count($field = '') {
        $this->_defaultField($field);
        return $this->query->count($this->table_field);
    }

    /**
     * Sum统计查询
     * @param string|array|Expression $field 查询字段
     * @return float|string
     */
    public function _sum($field = '') {
        $this->_defaultField($field);
        return $this->query->sum($this->table_field);
    }

    /**
     * Avg统计查询
     * @param string|array|Expression $field 查询字段
     * @return float|string
     */
    public function _avg($field = '') {
        $this->_defaultField($field);
        return $this->query->avg($this->table_field);
    }

    /**
     * 分页查询
     * @param boolean $is_total 是否集合分页记录数
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return array
     */
    public function _page($is_total = false, $fields = 'page,pagesize', $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $page_arr[$qk] = $data[$qk];
            }
        }
        list($page, $pagesize) = [$page_arr['page'] ?? 1, $page_arr['pagesize'] ?? 10];
        $is_total && $total = $this->_count();
        $this->query->page($page, $pagesize);
        $list = $this->_select();
//        print_r($this->_getLastSql());die;
//        return !$is_total ? $list : ['list' => $list, 'total' => $total];
        return !$is_total ? $list : $this->toPage($list, $total, $page, $pagesize);
    }

    /**
     * 设置Equal查询条件
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _equal($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->query->where($dk, "{$data[$qk]}");
            }
        }
        return $this;
    }

    /**
     * 设置Like查询条件
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _like($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->query->whereLike($dk, "%{$data[$qk]}%");
            }
        }
        return $this;
    }

    /**
     * 设置orLike查询条件
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _orLike($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $where[] = [$dk, 'like', "%{$data[$qk]}%"];
            }
        }
        !empty($where) && $this->query->where(function($query) use($where) {
                $query->whereOr($where);
            });
        return $this;
    }

    /**
     * 设置IN区间查询
     * @param string $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _in($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->query->whereIn($dk, explode($split, $data[$qk]));
            }
        }
        return $this;
    }

    /**
     * 设置FIND_IN_SET查询条件
     * @param string $fields 查询字段
     * @param string $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _findInSet($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                if (stripos($data[$qk], $split) !== false) {
                    foreach (explode($split, $data[$qk]) as $qk_val) {
                        $where[] = ['', 'EXP', Db::raw("FIND_IN_SET('{$qk_val}',{$dk})")];
                    }
                    !empty($where) && $this->query->where(function($query) use($where) {
                            $query->whereOr($where);
                        });
                } else {
                    $where[] = ['', 'EXP', Db::raw("FIND_IN_SET('{$data[$qk]}',{$dk})")];
                    $this->query->where($where);
                }
            }
        }
        return $this;
    }

    /**
     * 设置Order查询条件
     * @param string $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _order($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->query->order([$dk => $data[$qk]]);
            }
        }
        return $this;
    }

    /**
     * 设置Rand查询条件
     * @param int $num 数量
     * @return $this
     */
    public function _rand($num = 0) {
        $this->query->orderRaw('rand()');
        $num && $this->query->limit($num);
        return $this;
    }

    /**
     * 设置区域查询条件或大于等于
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _valueBetweenOrEgt($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                if (stripos($data[$qk], '|') !== false) {
                    foreach (explode('|', $data[$qk]) as $qk_val) {
                        if (stripos($qk_val, $split) !== false) {
                            list($begin, $after) = explode($split, $qk_val);
                            $where[] = [$dk, 'between', $qk_val];
                        } else {
                            $where[] = [$dk, 'egt', $qk_val];
                        }
                    }
                    !empty($where) && $this->query->where(function($query) use($where) {
                            $query->whereOr($where);
                        });
                } else {
                    if (stripos($data[$qk], $split) !== false) {
                        list($begin, $after) = explode($split, $data[$qk]);
                        $this->query->whereBetween($dk, [$begin, $after]);
                    } else {
                        $this->query->where($dk, 'egt', $data[$qk]);
                    }
                }
            }
        }
        return $this;
    }

    /**
     * 设置内容区间查询
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _valueBetween($fields, $input = '', $split = ',', $alias = '#') {
        return $this->_setBetweenWhere($fields, $input, $split, $alias);
    }

    /**
     * 设置日期时间区间查询
     * @param string|array $fields 查询字段
     * @param string $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _dateBetween($fields, $input = '', $split = ',', $alias = '#') {
        return $this->_setBetweenWhere($fields, $input, $split, $alias, function ($value, $type) {
                if (preg_match('#^\d{4}(-\d\d){2}\s+\d\d(:\d\d){2}$#', $value)) {
                    return $value;
                } else {
                    return $type === 'after' ? "{$value} 23:59:59" : "{$value} 00:00:00";
                }
            });
    }

    /**
     * 设置时间戳区间查询
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _timeBetween($fields, $input = '', $split = ',', $alias = '#') {
        return $this->_setBetweenWhere($fields, $input, $split, $alias, function ($value, $type) {
                if (preg_match('#^\d{4}(-\d\d){2}\s+\d\d(:\d\d){2}$#', $value)) {
                    return strtotime($value);
                } else {
                    return $type === 'after' ? strtotime("{$value} 23:59:59") : strtotime("{$value} 00:00:00");
                }
            });
    }

    /**
     * 设置区域查询条件
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @param callable $callback
     * @return $this
     */
    private function _setBetweenWhere($fields, $input = '', $split = ',', $alias = '#', $callback = null) {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                list($begin, $after) = explode($split, $data[$qk]);
                if (is_callable($callback)) {
                    $after = call_user_func($callback, $after, 'after');
                    $begin = call_user_func($callback, $begin, 'begin');
                }
                $this->query->whereBetween($dk, [$begin, $after]);
            }
        }
        return $this;
    }

    /**
     * 输入参数
     * @param string|array $input 输入类型 get.|post.
     * @return array
     */
    public function _input($input = '') {
//        return is_array($input) && $input ? $input : input($input);
        if (is_array($input)) {
            return $input;
        }
        $param = input($input);
        ($member_id = member_id()) && $param['member_id'] = $member_id; 
        return $param;
    }

    /**
     * 获取请求数据
     * @param string $key 指定数据
     * @param string $default 默认值
     * @return array 
     */
    public function getRequestData($key = null, $default = '') {
        $data = $this->_input();
        return is_null($key) ? $data : ($data[$key] ?? $default);
    }

    /**
     * 静态实例对象
     * @param string $table
     * @return static
     */
    public static function instance($table = null) {
        return (new \ReflectionClass(static::class))->newInstance($table);
    }

    /**
     * 设置jsonLike查询条件
     * @param string|array $fields 查询字段
     * @param string|array $json_fields 查询json字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _jsonLike($fields, $json_fields = 'value', $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
                if (stripos($qk, '@') !== false) {
                    list($qk, $json_field) = explode('@', $qk);
                }
            } else {
                if (stripos($dk, '@') !== false) {
                    list($dk, $json_field) = explode('@', $dk);
                    $qk = $dk;
                }
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->query->where("JSON_EXTRACT({$dk}, '$[*]." . $json_fields . "') LIKE '%" . $data[$qk] . "%'");
            }
        }
        return $this;
    }

    /**
     * 设置orJsonLike查询条件
     * @param string|array $fields 查询字段
     * @param string|array $json_fields 查询json字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _orJsonLike($fields, $json_fields = 'value', $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
                if (stripos($qk, '@') !== false) {
                    list($qk, $json_field) = explode('@', $qk);
                }
            } else {
                if (stripos($dk, '@') !== false) {
                    list($dk, $json_field) = explode('@', $dk);
                    $qk = $dk;
                }
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->query->whereOr("JSON_EXTRACT({$dk}, '$[*]." . $json_fields . "') LIKE '%" . $data[$qk] . "%'");
            }
        }
        return $this;
    }

    /**
     * 设置orMultiLike查询条件
     * @param string|array $fields 查询字段
     * @param string|array $input 输入类型 get.|post.
     * @param string $split 输入分隔符
     * @param string $alias 别名分割符
     * @return $this
     */
    public function _orMultiLike($fields, $input = '', $split = ',', $alias = '#') {
        $data = $this->_input($input);
        foreach (is_array($fields) ? $fields : explode($split, $fields) as $field) {
            list($dk, $qk) = [$field, $field];
            if (stripos($field, $alias) !== false) {
                list($dk, $qk) = explode($alias, $field);
                if (stripos($qk, '|') !== false) {
                    list($qk, $method) = explode('|', $qk);
                    if (stripos($method, '@') !== false) {
                        list($method, $json_field) = explode('@', $method);
                    }
                }
            } else {
                if (stripos($dk, '|') !== false) {
                    list($dk, $method) = explode('|', $dk);
                    $qk = $dk;
                    if (stripos($method, '@') !== false) {
                        list($method, $json_field) = explode('@', $method);
                    }
                }
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                if (isset($method) && $method == 'json') {
                    $where[] = "JSON_EXTRACT({$dk}, '$[*]." . $json_field . "') LIKE '%" . $data[$qk] . "%'";
                } else {
                    $where[] = "{$dk} LIKE '%{$data[$qk]}%'";
                }
            }
        }
        !empty($where) && $this->query->where(implode(' OR ', $where));
        return $this;
    }

    /**
     * 原生execute
     * @param string $sql SQL语句
     * @param array $bind 参数绑定
     * @return mexid
     */
    public function _dBExecute($sql, $bind = []) {
        return (!$bind ? Db::execute($sql) : Db::execute($sql, $bind)) !== false;
    }

    /**
     * 原生query
     * @param string $sql SQL语句
     * @param array $bind 参数绑定
     * @return mexid
     */
    public function _dBQuery($sql, $bind = []) {
        return !$bind ? Db::query($sql) : Db::query($sql, $bind);
    }

    /**
     * 插入或更新
     * @param string $table 表名
     * @param array $data 数据
     * @return mexid
     */
    public function _insertInto($table, $data) {
        list($column_arr, $insert_arr, $update_arr, $sql) = [[], [], [], ""];
        count($data) == count($data, 1) && $data = [$data]; // 一维转二维
        foreach ($data as $k => $v) {
            !$column_arr && $column_arr = array_keys($v); // 表字段
            $insert_arr[] = "(" . implode(",", array_map(function ($val) {return "'{$val}'";}, $v)) . ")"; // 插入数据
        }
        foreach ($column_arr as $k => $v) $update_arr[] = "`{$v}` = VALUES(`{$v}`)"; // 更新数据
        list($column_str, $insert_str, $update_str) = [implode(',', array_map(function ($column) {return "`{$column}`";}, $column_arr)), implode(',', $insert_arr), implode(',', $update_arr)];
        if ($column_str && $insert_str && $update_str) {
            $sql = "INSERT INTO {$table} ({$column_str}) VALUES {$insert_str} ON DUPLICATE KEY UPDATE {$update_str}"; // 拼接sql
        }
        unset($column_str, $insert_str, $update_str, $column_arr, $insert_arr, $update_arr);
        return $this->_dBExecute($sql);
    }
    
    /**
     * 分页信息
     * @param array $list
     * @param int $count 总数
     * @param int $page 页数
     * @param int $pageSize 页数大小
     * @return array
     */
    public function toPage($list, $count = 0, $page = 1, $pageSize = 10) {
//        $page = (int) $page > 0 ? (int) $page : 1;
//        $pageSize = (int) $pageSize > 0 ? (int) $pageSize : 10;
        return [
            "list" => $list,
            "count" => $count,
            "pages" => ceil($count / $pageSize),
            "page" => $page,
            "limit" => $pageSize,
        ];
    }
    
    /**
     * 应用模块
     */
    public static function getModule() {
        return \think\facade\Request::instance()->rootUrl();
    }

}
